import router from './router';
//路由进度条
import nprogress from 'nprogress';
import "nprogress/nprogress.css"
//引入token
import pinia from './store/index'
import userStore from './store/modules/user';
let userData = userStore(pinia)
// console.log(userData, 122)
//全局前置守卫
router.beforeEach((to: any, from: any, next: any) => {
    //to：你将要访问那个路由
    //from：你从那个路由而来
    //next：路由放行函数
    nprogress.start();
    //获取token，判断用户登录，还是未登录
    let token = userData.token;
    //获取用户名字
    // let username = userData.username;
    if (token) {
        //登录成功
        if (to.path == '/login') {
            next({ path: '/' })
        } else {
            next()
        }
    } else {
        //用户未登录判断
        if (to.path == '/login') {
            next()
        } else {
            //指向未登录地址
            next({ path: '/login', query: { redirect: to.path } })
        }
    }

})
//全局后置守卫
router.afterEach((to: any, from: any) => {
    nprogress.done();
})